//
//  ModuleView.m
//  Project
//
//  Created by 冯东辉 on 2018/9/18.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "ModuleView.h"
#import "ModuleBtn.h"

@implementation ModuleView

- (id)initWithFrame:(CGRect)frame data:(NSArray *)data {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self initSubViewWithData:data];
    }
    
    return self;
}

- (void)initSubViewWithData:(NSArray *)data {
    
    NSInteger dataCount = [data count];
    
    NSInteger count = 0;
    
    if (dataCount % 4 == 0) {
        
        count = dataCount/4;
    }else {
        count = dataCount/4 + 1;
    }
    
    CGFloat view_width = GET_SCREEN_WIDTH()/4.0f;
    
    CGFloat view_height = self.height/count;
    
    CGFloat view_top = 0.0f;
    CGFloat view_left = 0.0f;
    
    for (int i = 0; i < [data count]; i++) {
        
        NSDictionary *dic = [data objectAtIndex:i];
        
        if (i % 4 == 0 && i != 0) {
            view_left = 0.0f;
            view_top = view_top + view_height;
            
        }else {
            
            if (i != 0) {
                
                view_left = view_left + view_width;
            }
        }
        
        ModuleBtn *btn = [[ModuleBtn alloc] initWithFrame:CGRectMake(view_left, view_top, view_width, view_height)];
        btn.dim_Name = dic[@"dim_Name"];
        btn.sdim_ID = dic[@"sdim_ID"];
        [btn setImage:[UIImage imageNamed:btn.dim_Name] forState:UIControlStateNormal];
        [btn setTitle:btn.dim_Name forState:UIControlStateNormal];
        [btn setBackgroundColor:UICOLOR_HEX(0xffffffff)];
        [btn setTitleColor:UICOLOR_HEX(0xff666666) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        btn.tag = i+100;
        [btn addTarget:self action:@selector(ModuleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        NSInteger redCount = [dic[@"count"] integerValue];
        
        CGFloat image_right = (btn.width-btn.imageView.width)/2.0f+btn.imageView.width;
        CGFloat image_top   = (btn.height-btn.imageView.height)/2.0f-10;
        
        UILabel *redLabel = [[UILabel alloc] initWithFrame:CGRectMake(image_right-btn.imageView.width/8.0f-8, image_top+btn.imageView.height/8.0f-8, 16, 16)];
        redLabel.tag = 1000+i;
        redLabel.layer.cornerRadius = 8.0f;
        redLabel.layer.masksToBounds = YES;
        redLabel.text = [NSString stringWithFormat:@"%ld", redCount];
        redLabel.backgroundColor = UICOLOR_HEX(0xffff6767);
        redLabel.textAlignment = NSTextAlignmentCenter;
        redLabel.font = [UIFont systemFontOfSize:11];
        redLabel.textColor = UICOLOR_HEX(0xffffffff);
        [btn addSubview:redLabel];
        
        if (redCount == 0) {
            redLabel.hidden = YES;
        }
    }
}

- (void)reloadViewWithData:(NSMutableArray *)data {
    
    for (int i = 0; i < [data count]; i++) {
        
        NSDictionary *dic = [data objectAtIndex:i];
        NSInteger redCount = [dic[@"count"] integerValue];
        
        UIButton *btn = [self viewWithTag:100+i];
        UILabel *label = [btn viewWithTag:1000+i];
        
        label.text = [NSString stringWithFormat:@"%ld", redCount];
        
        if (redCount == 0) {
            label.hidden = YES;
        }else {
            label.hidden = NO;
        }
    }
}

- (void)ModuleBtnClick:(ModuleBtn *)btn {
    
    if ([_delegate respondsToSelector:@selector(touchModuleBtnCallBack:note:)]) {
        
        [_delegate touchModuleBtnCallBack:btn.dim_Name note:btn.sdim_ID];
    }
}


@end
